$(function(){
	var admin_id = getCookie('admin_id');
	if(!admin_id){
		window.location.replace('login.html');
	}
	var elem = document.querySelector('.js-switch');
    var switchery = new Switchery(elem, {
        color: '#1AB394'
    });
    //业务开始
    var id = getQueryString('id');
    if(!id){
    	swal("操作错误", "参数传递错误！", "error");
    	setTimeout(function(){
             location.href="member_list.html";
		},3000);
		return false;
    }
    //获取基本信息
    $.ajax({
		url: ApiUrl + "/member/"+id,
        type: 'get',
        dataType: 'JSON',
        success: function(result) {
        	if(result.code == 200){
        		document.getElementById("real_name").value = result.data.real_name;
        		document.getElementById("phone").value = result.data.phone;
        		document.getElementById("number").value = result.data.number;
        		document.getElementById("memo").value = result.data.memo;
        		if(result.data.status == 1){
        			$("#site_status1").attr("checked","checked");
					$("#site_status0").removeAttr("checked");
					$(".cb-enable").addClass('selected');
  					$(".cb-disable").removeClass('selected');
        		}else{
        			$("#site_status0").attr("checked","checked");
					$("#site_status1").removeAttr("checked");
					$(".cb-enable").removeClass('selected');
  					$(".cb-disable").addClass('selected');
        		}
        	}else{
        		parent.layer.msg(result.message);
        		return false;
        	}
        }
   });
	//自定义radio样式
	$(".cb-enable").click(function(){
	    var parent = $(this).parents('.onoff');
	    $('.cb-disable',parent).removeClass('selected');
	    $(this).addClass('selected');
	    $('.checkbox',parent).attr('checked', true);
	});
	$(".cb-disable").click(function(){
	    var parent = $(this).parents('.onoff');
	    $('.cb-enable',parent).removeClass('selected');
	    $(this).addClass('selected');
	    $('.checkbox',parent).attr('checked', false);
	});
	//提交表单
   $("#submit").click(function(){
   		 if($("#signupForm").valid()){
          var query = $("#signupForm").serialize();
          ajax_post(ApiUrl + "/member/"+id,"member_list.html",query);
    	}
   });
   var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            },
            phone: {
                required: true,
                number : true,
                rangelength:[11,11]
            }
        },
        messages: {
            name: {
                required: icon+ "请输入会员名称！"
            },
            phone: {
                required: icon+ "请输入手机号！",
                number :  icon+"手机号只能是数字" ,
                rangelength: icon+"请输入正确的手机号"
            }
        }
    });
});